class code4_2 {
    public int minDepth(TreeNode root) {
        if(root == null) return 0;

        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        int level = 0;

        while(!q.isEmpty()) {
            level++;
            int sz = q.size();
            for(int i=0;i<sz;i++) {
                TreeNode x = q.poll();
                //如果当前node节点的左右子树都为空，直接返回level即可
                if(x.left == null && x.right == null) {
                    return level;
                }
                //左右子节点，哪个不为空，哪个加入到队列中
                if(x.left != null) {
                    q.offer(x.left);
                }
                if(x.right != null) {
                    q.offer(x.right);
                }
            }
        }
        return 0;
    }
}